//
//  DrawerViewController.m
//  老司机发车了
//
//  Created by 斌 on 16/8/9.
//  Copyright © 2016年 斌. All rights reserved.
//

#import "DrawerViewController.h"

@interface DrawerViewController ()

//平移手势
@property (nonatomic,strong) UIPanGestureRecognizer *pan;


@end

@implementation DrawerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

//初始化方法 第一个参数 左侧抽屉控制器 第二个 主页控制器
-(instancetype)initWithLeftViewController:(UIViewController *)leftVireController MainViewController:(UIViewController *)MainViewController
{
    self = [super init];
    if (self) {
        //添加为子视图控制器 如果在一个控制器A里使用了 另外一个控制器B的View 需要把这个控制器B添加为控制器的子视图控制器 防止控制器被销毁
        [self addChildViewController:leftVireController];
        [self addChildViewController:MainViewController];
        
        //左侧控制器添加在最下面
        [self.view addSubview:leftVireController.view];
        //在添加主页控制器view
        [self.view addSubview:MainViewController.view];
        //视图赋值
        self.mainView = MainViewController.view;
        self.leftView = leftVireController.view;
        
        //初始化平移手势
        self.pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panHander)];
        //添加到上面的MainView上
        [self.mainView addGestureRecognizer:self.pan];
        
        
        
        //将leftView的frame修改到屏幕左边 来做一个平移效果
        CGRect leftFrame = self.leftView.frame;
        leftFrame.origin.x -= self.view.frame.size.width * kproportion / 2;
        self.leftView.frame = leftFrame;
        
        //先把缩放设置为2 也就是放大状态
//        self.leftView.transform = CGAffineTransformMakeScale(2, 2);
        
        
        //给导航控制器设置代理
        UINavigationController *navc = (UINavigationController *)MainViewController;
        navc.delegate = self;
        
    }
    return self;
    
}

//平移手势处理方法
- (void)panHander{
    //获取用户手指位置
    CGPoint userPoint = [self.pan translationInView:self.mainView];
    //接受原始的frame
    CGRect mainFrame = self.mainView.frame;
    mainFrame.origin.x += userPoint.x;
    //判断是否是在可滑动的范围内
    if (mainFrame.origin.x >= 0 && mainFrame.origin.x <= self.view.frame.size.width * kproportion) {
        self.mainView.frame = mainFrame;
        //修改缩放
        //获得当前偏移的比例
//        CGFloat a = mainFrame.origin.x / (self.view.frame.size.width * kproportion);
//        self.leftView.transform = CGAffineTransformMakeScale(2 - a, 2 - a);
        
        //同时修改leftView的frame
        CGRect leftFrame = self.leftView.frame;
        leftFrame.origin.x = mainFrame.origin.x / 2 - self.view.frame.size.width * kproportion / 2;
        self.leftView.frame = leftFrame;
    }
    //将本次已经移动到的点设置为0
    [self.pan setTranslation:(CGPointZero) inView:self.mainView];
    
    //当用户手指离开屏幕的时候判断需要打开还是关闭
    if (self.pan.state == UIGestureRecognizerStateEnded) {
        //大于一半调用open
        if (self.mainView.frame.origin.x >= self.view.frame.size.width * kproportion / 2) {
            [self open];
        }else{
            //否则调用close
            [self close];
        }
    }
}


//打开抽屉
- (void)open{
    [UIView animateWithDuration:0.3 animations:^{
        //修改leftView的缩放
//        self.leftView.transform = CGAffineTransformMakeScale(1, 1);
        
        //获取原始frame
        CGRect mainFrame = self.mainView.frame;
        //修改X值 使其往右边移动
        mainFrame.origin.x = self.view.frame.size.width * kproportion;
        //赋给View新的frame
        self.mainView.frame = mainFrame;
        
        //修改leftView的frame让他跟着一起动
        self.leftView.frame = self.view.frame;
    }completion:^(BOOL finished) {
        self.isOpen = YES;
    }];
}
//关闭抽屉
- (void)close{
    [UIView animateWithDuration:0.3 animations:^{
        //修改leftView的缩放
//        self.leftView.transform = CGAffineTransformMakeScale(2, 2);
        
        
        //让主页视图回到原始位置
        self.mainView.frame = self.view.frame;
        
        //让leftView在回到屏幕左边去
        CGRect leftFrame = self.leftView.frame;
        leftFrame.origin.x -= self.view.frame.size.width * kproportion / 2;
        self.leftView.frame = leftFrame;
        
        
    }completion:^(BOOL finished) {
        //修改状态
        self.isOpen = NO;
    }];
}



//切换主视图
-(void)setMainViewController:(UIViewController *)newVc{
    //给导航控制器设置代理
    UINavigationController *navc = (UINavigationController *)newVc;
    navc.delegate = self;
    
    
    
    //判断是否已经添加这个控制器作为子视图控制器
    if (![self.childViewControllers containsObject:newVc]) {
        [self addChildViewController:newVc];
    }
    //如果传进来的控制器正在显示 直接返回
    if (newVc.view == self.mainView) {
        return;
    }
    
    //用当前主视图的frame作为新的视图的frame
    CGRect mainFeame = self.mainView.frame;
    //先让将要出现的view处于屏幕的下方
    mainFeame.origin.y += kScreenHeight;
    
    newVc.view.frame = mainFeame;
    
    //第二个要去的地方是原来的位置
    mainFeame.origin.y -= kScreenHeight;
    
    //添加到视图上
    [self.view addSubview:newVc.view];
    
    CGRect goFrame = mainFeame;
    goFrame.origin.y -= kScreenHeight;
    
    //加点特效
    [UIView animateWithDuration:0.3 animations:^{
        newVc.view.frame = mainFeame;
        //让原来的view滑到上面去
        self.mainView.frame = goFrame;
        self.leftView.userInteractionEnabled = NO;
    }completion:^(BOOL finished) {
        //做完动画再将原来的view删除
        //移除旧的视图
        [self.mainView removeFromSuperview];
        //移除平移手势
        [self.mainView removeGestureRecognizer:self.pan];
        //让MainView的指针指向新的MainView
        self.mainView = newVc.view;
        //给新的mainView添加手势
        [self.mainView addGestureRecognizer:self.pan];
        self.leftView.userInteractionEnabled = YES;
        //切换完成 关闭抽屉
        [self close];
    }];
}


-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    //如果将要显示的vc 就是rootVC 把手势添加回来 如果不是 把手势删掉
    if (navigationController.childViewControllers[0] == viewController) {
        [self.mainView addGestureRecognizer:self.pan];
    }else{
        [self.mainView removeGestureRecognizer:self.pan];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
